package com.osig.ec.cfca;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;



import cfca.paperless.client.bean.FieldBean;
import cfca.paperless.client.bean.MultiDataBean;
import cfca.paperless.client.bean.PdfBean;
import cfca.paperless.client.bean.SealUserInfo;
import cfca.paperless.client.bean.SignLocation;
import cfca.paperless.client.servlet.PaperlessUniteClient;
import cfca.paperless.client.util.IoUtil;
import cfca.paperless.client.util.PaperlessUtil;
import cfca.paperless.client.util.StrategyUtil;

/**
 * @Description 联合接口(合成业务模板,合成业务数据,自动化签章)
 * @author clsu
 * @CodeReviewer TODO
 */
public class testPaperlessPeople {

	public static void main(String[] args) throws Exception {
		try {
			long timeStart = new Date().getTime();// 开始时间

			PaperlessUniteClient clientBean = new PaperlessUniteClient(PaperlessConfig.uniteUrl);
			////////////////////////////////// 合成模板(synthesizeAutoTemplate)///////////////////////////////////
			String templateCode = "mb001";
			String fieldBeanListXml = PaperlessUtil.buildFieldBeanListXml(getFieldBeanList());
			String textBeanListXml = "";
			String imageBeanListXml = "";
			clientBean.addSynthesizeAutoTemplateParams(templateCode, fieldBeanListXml, textBeanListXml,
					imageBeanListXml);

			/////////////////////////// 如下为用户1手写签名相关的数据，如果没用用户1此处可以省略/////////////////////////////////////////////////
			byte[] handwritingImage1 = IoUtil.read(new FileInputStream("/usr/local/tomcat-dzht/webapps/econtract-web/static/autoGenerateImage.png"));
			byte[] handwritingData1 = handwritingImage1;

			List<MultiDataBean> list1 = new ArrayList<MultiDataBean>();
			list1.add(new MultiDataBean(handwritingData1, "handwritingData.dat", "media-id", "0"));// 手写签名数据
			list1.add(new MultiDataBean(handwritingImage1, "handwritingImage.png", "2"));// 手写签名图片
	
			String proofHashAndMultiData1[] = PaperlessUtil.getProofHashAndMultiData(list1);

			SignLocation signLocation = new SignLocation(1, "项目", "R", 10, 0);// r
																				// <LocationStyle>
																				// 位置风格：上:U；下:D；左:L；右:R；中:C；默认：C，必填

			// z 证件类型
			SealUserInfo sealUserInfo = new SealUserInfo("宋江", "Z", "1234567876543", "15744659823", "北京", "签合同");
			String proofSealStrategyXml = StrategyUtil.createProofSealStrategyXml(handwritingImage1, sealUserInfo,
					signLocation, proofHashAndMultiData1[0]);

			/////////////////////////// 以上为用户1的数据/////////////////////////////////////////////////
			String compoundSealStrategyXml = "<List>" + proofSealStrategyXml + "</List>";
			String bizTypeCode = "";// 业务类型，比如：开户、开通网银
			String outputFilePath = "/usr/local/tomcat-dzht/webapps/econtract-web/static/httest201706161456.pdf";
			PdfBean pdfBean = new PdfBean("", "a", bizTypeCode, outputFilePath);// 系统自动存储,存储的路径自动生成
			String multiData = "";
			String pdfBeanXml = PaperlessUtil.buildPdfBeanXml(pdfBean);
			clientBean.addCompoundSealAutoPdfParams(pdfBeanXml, multiData, compoundSealStrategyXml);

			/////////////////////////////////////////////////////////////////////////////////////////

			// 操作员编码或者机构号
			String operatorCode = PaperlessConfig.operatorCode;
			// 渠道编码，可以为空
			String channelCode = PaperlessConfig.channelCode;

			byte[] resultData = clientBean.uniteReqAndRes(operatorCode, channelCode);

			String errorRsString = PaperlessUtil.isError(resultData);

			// 处理结果为正常，保存签章后的PDF文件到本地目录下
			if ("".equals(errorRsString)) {
				System.out.println("OK:sealAutoPdf");
				System.out.println(resultData.length);
				System.out.println(outputFilePath);

				// IoUtil.write("C:\\Users\\Pengfei\\Desktop\\test201706161100.pdf",
				// resultData);
			} else {
				// 处理结果为异常，打印出错误码和错误信息
				System.out.println("NG:sealAutoPdf");
				System.out.println(resultData.length);
				System.out.println(new String(resultData, "UTF-8"));
			}

			long timeEnd = new Date().getTime();// 结束时间
			System.out.println("time:" + (timeEnd - timeStart));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private static List<FieldBean> getFieldBeanList() throws Exception {
		List<FieldBean> fieldBeanList = new ArrayList<FieldBean>();

		fieldBeanList.add(new FieldBean("contractNo", "zzxf1234567890", "", "text"));
		fieldBeanList.add(new FieldBean("IDcard", "zzxf1234567890", "", "text"));
		fieldBeanList.add(new FieldBean("address", "宜宾市商业银行", "", "text"));
		fieldBeanList.add(new FieldBean("phone", "zzxf1234567890", "", "text"));
		fieldBeanList.add(new FieldBean("capital", "壹拾贰万叁仟肆佰伍拾陆圆整行", "", "text"));
		fieldBeanList.add(new FieldBean("small", "123456", "", "text"));
		fieldBeanList.add(new FieldBean("deadline", "2019.12.11", "", "text"));
		fieldBeanList.add(new FieldBean("rate", "20", "", "text"));

		return fieldBeanList;
	}
}
